package com.kuaiditu.assistant.ui.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.kuaiditu.assistant.R;
import com.kuaiditu.assistant.util.DisplayHelper;

/**
 * Created by Eenie on 2017/9/20.
 * Email: eeniework@gmail.com
 * Des:
 */

@Route(path = BottomDialog.PATH)
public class BottomDialog extends DialogFragment implements View.OnClickListener {
    public static final String PATH = "/dialog/choice_pic";
    public static final int CHOICE_TOOK =0;
    public static final int CHOICE_GALLERY = 1;
    private float dimAmount = 0.5f;
    private boolean showBottom = true;
    private int animStyle = 0;
    private int height = 0;
    private int width = 0;
    private boolean outCancel = true;
    private int margin;




    private OnClickListener mClickListener;


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_choice_pic, container, false);


        view.findViewById(R.id.tv_took).setOnClickListener(this);
        view.findViewById(R.id.tv_gallery).setOnClickListener(this);
        view.findViewById(R.id.tv_cancel).setOnClickListener(this);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        initParams();//初始化弹窗的参数
    }

    private void initParams() {
        Window window = getDialog().getWindow();
        if (window != null) {
            WindowManager.LayoutParams lp = window.getAttributes();
            //调节灰色背景透明度[0-1]，默认0.5f
            lp.dimAmount = dimAmount;
            //是否在底部显示
            if (showBottom) {
                lp.gravity = Gravity.BOTTOM;
                if (animStyle == 0) {
                    animStyle = R.style.DefaultAnimation;
                }
            }
            //设置dialog宽度
            if (width == 0) {
                lp.width = DisplayHelper.getScreenWidth(getActivity()) - 2 * DisplayHelper.dp2px(getActivity(), margin);
            } else {
                lp.width = DisplayHelper.dp2px(getActivity(), width);
            }
            //设置dialog高度
            if (height == 0) {
                lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            } else {
                lp.height = DisplayHelper.dp2px(getActivity(), height);
            }
            //设置dialog进入、退出的动画
            window.setWindowAnimations(animStyle);
            window.setAttributes(lp);
        }
        setCancelable(outCancel);//设置点击外部是否消失
    }

    @Override
    public void onClick(View v) {
        if (mClickListener != null) {
            switch (v.getId()) {

                case R.id.tv_took:
                    mClickListener.onChoiceItem(CHOICE_TOOK);
                    break;

                case R.id.tv_gallery:
                    mClickListener.onChoiceItem(CHOICE_GALLERY);
                    break;

                case R.id.tv_cancel:
                    mClickListener.onCancel();
                    break;
            }
        }
        dismiss();
    }


    public interface OnClickListener {

        void onChoiceItem(int pos);


        void onCancel();

    }

    public void setClickListener(OnClickListener clickListener) {
        mClickListener = clickListener;
    }
}
